//
//  MYGMincrostationTopView.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/5/23.
//

import UIKit

class MYGMincrostationTopView: UIView {
    var model = MYGFriendsModel()
    var selectIndex:((_ index: Bool)->Void)?
    var amodel:MYGAboutGDotorModel? {
        didSet {
            guard let item = amodel else {return}
            
            helpNumLabel.rz.colorfulConfer { confer in
                confer.text("\(item.patient)")?.font(.regular(13)).textColor(.rgba(51, 51, 51,1))
                confer.text("\n患者")?.font(.regular(13)).textColor(.rgba(159, 159, 159,1))
            }

            askNumLabel.rz.colorfulConfer { confer in
                confer.text("\(item.following)")?.font(.regular(13)).textColor(.rgba(51, 51, 51,1))
                confer.text("\n粉丝")?.font(.regular(13)).textColor(.rgba(159, 159, 159,1))
            }

            fllowLabel.rz.colorfulConfer { confer in
                confer.text("\(item.followers)")?.font(.regular(13)).textColor(.rgba(51, 51, 51,1))
                confer.text("\n关注")?.font(.regular(13)).textColor(.rgba(159, 159, 159,1))
            }
            
        }
        
    }
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
        
        aboutBtn.isSelected = true
        addBtn.isHidden = true
        shareBtn.isHidden = true
        chatBtn.isHidden = true
        
        
    }
    
    func setViewContent(model: MYGFriendsModel) {
        
        if (model.id.isEmpty){
            doctorNameLabel.text = MYGUserManager.getUser()?.doctor.name
            doctorImageView.sd_setImage(with: URL(string: MYGUserManager.getUser()?.doctor.avatar), placeholderImage: UIImage(named: "MYG_myPage_touxiang"), context: nil)
            hosNameLabel.text = MYGUserManager.getUser()?.doctor.hospital
            keshiLabel.text = MYGUserManager.getUser()?.doctor.department
            aboutBtn.setTitle("关于我", for: .normal)
            aboutMineBtn.setTitle("我的动态", for: .normal)
            followBtn.isHidden = true
        }else {
            self.model = model
            doctorNameLabel.text = model.from_doctor.name
            doctorImageView.sd_setImage(with: URL(string: model.from_doctor.avatar), placeholderImage: UIImage(named: "MYG_myPage_touxiang"), context: nil)
            hosNameLabel.text = model.from_doctor.hospital
            keshiLabel.text = model.from_doctor.department
            aboutBtn.setTitle("关于他", for: .normal)
            aboutMineBtn.setTitle("他的动态", for: .normal)
//            shareBtn.setTitle("关注", for: .normal)
            chatBtn.isHidden = model.from_doctor.phone.isEmpty
//            shareBtn.isHidden = true
//            addBtn.isHidden = false
            followBtn.isHidden = false
        }

    }
    
    @objc func changeButton(_ sender : UIButton){
        if sender == aboutMineBtn {
            aboutBtn.isSelected = false
            aboutMineBtn.isSelected = true
            self.selectIndex?(false)
            UIView.animate(withDuration: 0.5) { [self] in
                aboutLineView.jf_centerX = aboutMineBtn.jf_centerX
            }
        }else {
            aboutBtn.isSelected = true
            aboutMineBtn.isSelected = false
            self.selectIndex?(true)
            UIView.animate(withDuration: 0.5) { [self] in
                aboutLineView.jf_centerX = aboutBtn.jf_centerX
            }
        }
    }

    @objc func clickChatBtn() {
        if ((MYGUserManager.getUser()?.doctor.name.isEmpty) == true) || MYGUserManager.getUser()?.doctor.gender == 0  || ((MYGUserManager.getUser()?.doctor.identity.isEmpty) == true) {
            let vc = MYGBaseInfoController()
            UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            return
        }
        
        if MYGUserManager.isDoctorInfComplete  {
            if MYGUserManager.getUser()?.doctor.status == 20 {
                let phone = "\(self.model.from_doctor.phone)1"
                let vc = MYGChatController(sessionId: 0 ,mobile: phone,title: self.model.from_doctor.name)
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }else {
                MMYGHUD.showToast(text: "正在审核中，暂时无法使用该功能")
            }
    
        }else {
            let vc = MYGUpdateInfoViewController()
            vc.model = MYGUserManager.getUser()!
            vc.isEdit = true
            UIViewController.currentViewController?.navigationController?.pushViewController(vc)
        }
        
     
    }
    
    @objc func clickAddFriendBtn() {
      
        
    }
    
    @objc func clickFollowBtn() {
        
        
        MYGLoading.show()
        MMYGNetwork<MYGCommonAPI,MYGEmptyModel>.request(target: .followAction(id: "\(self.model.from_doctor.id)", prefix: "followUpDoctor")) { [self] _ in
            MYGLoading.hide()
            followBtn.isSelected = true
            
        }  failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
        
    }
    
    @objc func sendMessageBtn(){
        
    }
    func configUI(){
        
        addSubview(topbackImageV)
        topbackImageV.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(SCALEWIDTH(254))
        }
        addSubview(doctorImageView)
        doctorImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(15))
            make.top.equalToSuperview().offset(SCALEWIDTH(173))
            make.size.equalTo(CGSize(width: SCALEWIDTH(66), height: SCALEWIDTH(66)))
        }
        addSubview(doctorNameLabel)
        doctorNameLabel.snp.makeConstraints { make in
            make.left.equalTo(doctorImageView.snp.right).offset(SCALEWIDTH(11))
            make.top.equalToSuperview().offset(SCALEWIDTH(181.5))
            make.height.equalTo(SCALEWIDTH(28))
        }
        addSubview(keshiLabel)
        keshiLabel.snp.makeConstraints { make in
            make.left.equalTo(doctorNameLabel.snp.right).offset(SCALEWIDTH(10))
            make.centerY.equalTo(doctorNameLabel)
            make.height.equalTo(SCALEWIDTH(18))
        }
        addSubview(adressImageV)
        adressImageV.snp.makeConstraints { make in
            make.left.equalTo(doctorImageView.snp.right).offset(SCALEWIDTH(10))
            make.top.equalTo(doctorNameLabel.snp.bottom).offset(2)
            make.size.equalTo(CGSize(width: SCALEWIDTH(9), height: SCALEWIDTH(13)))
        }
        addSubview(hosNameLabel)
        hosNameLabel.snp.makeConstraints { make in
            make.left.equalTo(adressImageV.snp.right).offset(SCALEWIDTH(5))
            make.centerY.equalTo(adressImageV)
            make.height.equalTo(SCALEWIDTH(18))
        }
        addSubview(chatBtn)
        chatBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-SCALEWIDTH(24))
            make.top.equalToSuperview().offset(SCALEWIDTH(191.5))
            make.width.height.equalTo(SCALEWIDTH(31))
        }
        addSubview(shareBtn)
        shareBtn.snp.makeConstraints { make in
            make.right.equalTo(chatBtn.snp.left).offset(-14)
            make.top.equalToSuperview().offset(SCALEWIDTH(191.5))
            make.width.height.equalTo(SCALEWIDTH(31))
        }
        
        addSubview(helpNumLabel)
        helpNumLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(26))
            make.top.equalTo(topbackImageV.snp.bottom).offset(SCALEWIDTH(20))
            make.height.equalTo(SCALEWIDTH(40))
        }
        addSubview(askNumLabel)
        askNumLabel.snp.makeConstraints { make in
            make.left.equalTo(helpNumLabel.snp.right).offset(SCALEWIDTH(40))
            make.top.equalTo(topbackImageV.snp.bottom).offset(SCALEWIDTH(20))
            make.height.equalTo(SCALEWIDTH(40))
        }
        addSubview(fllowLabel)
        fllowLabel.snp.makeConstraints { make in
            make.left.equalTo(askNumLabel.snp.right).offset(SCALEWIDTH(40))
            make.top.equalTo(topbackImageV.snp.bottom).offset(SCALEWIDTH(20))
            make.height.equalTo(SCALEWIDTH(40))
        }
        addSubview(followBtn)
        followBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-SCALEWIDTH(10))
            make.top.equalTo(topbackImageV.snp.bottom).offset(SCALEWIDTH(24.5))
            make.size.equalTo(CGSize(width: SCALEWIDTH(79), height: SCALEWIDTH(32)))
        }
        
        addSubview(addBtn)
        addBtn.snp.makeConstraints { make in
            make.right.equalTo(followBtn.snp.left).offset(-SCALEWIDTH(10))
            make.top.equalTo(topbackImageV.snp.bottom).offset(SCALEWIDTH(24.5))
            make.size.equalTo(CGSize(width: SCALEWIDTH(32), height: SCALEWIDTH(32)))
        }
        
        addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.top.equalTo(helpNumLabel.snp.bottom).offset(SCALEWIDTH(10.5))
            make.height.equalTo(SCALEWIDTH(0.5))
            make.left.right.equalToSuperview()
        }
        
        addSubview(aboutBtn)
        aboutBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(24.5))
            make.top.equalTo(lineView.snp.bottom).offset(SCALEWIDTH(11))
            make.height.equalTo(SCALEWIDTH(22.5))
        }
        addSubview(aboutMineBtn)
        aboutMineBtn.snp.makeConstraints { make in
            make.left.equalTo(aboutBtn.snp.right).offset(SCALEWIDTH(24.5))
            make.top.equalTo(lineView.snp.bottom).offset(SCALEWIDTH(11))
            make.height.equalTo(SCALEWIDTH(22.5))
        }
        
        addSubview(aboutLineView)
        aboutLineView.snp.makeConstraints { make in
            make.centerX.equalTo(aboutBtn)
            make.top.equalTo(aboutBtn.snp.bottom).offset(SCALEWIDTH(8))
            make.size.equalTo(CGSize(width: SCALEWIDTH(16), height: SCALEWIDTH(3)))
        }
        
    }
    
    private lazy var topbackImageV:UIImageView = {
        let vi = UIImageView()
        vi.contentMode = .scaleAspectFill
        vi.image = UIImage(named: "MYG_doctorDetailBack")
        return vi
    }()
    
    private lazy var doctorImageView:UIImageView = {
        let vi = UIImageView()
        vi.contentMode = .scaleAspectFill
        vi.backgroundColor = .green
//        vi.sd_setImage(with: URL(string: "https://img0.baidu.com/it/u=3838093562,4126749835&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1709830800&t=d50dea440fb1f55112244c183a226ea9"))
        vi.layer.cornerRadius = SCALEWIDTH(33)
        vi.layer.masksToBounds = true
        return vi
    }()
    lazy var doctorNameLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(22)
        cont.textColor = .rgba(255, 255, 255)
       
        return cont
    }()
    private lazy var adressImageV:UIImageView = {
        let vi = UIImageView()
        vi.image = UIImage(named: "dorctorDetail_address")
        return vi
    }()
    lazy var hosNameLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(13)
        cont.textColor = .rgba(255, 255, 255,0.7)
        return cont
    }()
    lazy var keshiLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(13)
        cont.textColor = .rgba(255, 255, 255,0.7)
        return cont
    }()
    
    lazy var shareBtn:UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "Icon(1)"), for: .normal)
        cont.addTarget(self, action: #selector(sendMessageBtn), for: .touchUpInside)
        return cont
    }()
    
    
    lazy var chatBtn:UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "dorctorDetail_chat"), for: .normal)
        cont.addTarget(self, action: #selector(clickChatBtn), for: .touchUpInside)
        return cont
    }()
    
    lazy var helpNumLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(13)
        cont.textColor = .rgba(51, 51, 51,1)
        cont.numberOfLines = 2
        cont.textAlignment = .center
        cont.text = "0\n患者"
        let attributedString = NSMutableAttributedString(string: "0\n患者")
        attributedString.addAttribute(.foregroundColor, value: RGBA(r: 159, g: 159, b: 159, a: 1), range: NSRange(location: "0\n患者".count - 4, length: 4))
        cont.attributedText = attributedString
        return cont
    }()
    lazy var askNumLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(13)
        cont.textColor = .rgba(51, 51, 51,1)
        cont.numberOfLines = 2
        cont.textAlignment = .center
        cont.text = "0\n粉丝"
        let attributedString = NSMutableAttributedString(string: "0\n粉丝")
        attributedString.addAttribute(.foregroundColor, value: RGBA(r: 159, g: 159, b: 159, a: 1), range: NSRange(location: "0\n粉丝".count - 4, length: 4))
        cont.attributedText = attributedString
        return cont
    }()
    lazy var fllowLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(13)
        cont.textColor = .rgba(51, 51, 51,1)
        cont.numberOfLines = 2
        cont.textAlignment = .center
        cont.text = "0\n关注"
        let attributedString = NSMutableAttributedString(string: "0\n关注")
        attributedString.addAttribute(.foregroundColor, value: RGBA(r: 159, g: 159, b: 159, a: 1), range: NSRange(location: "0\n关注".count - 4, length: 4))
        cont.attributedText = attributedString
        return cont
    }()
    lazy var addBtn: UIButton = {
        let cont = UIButton(frame: CGRectMake(0, 0, SCALEWIDTH(32), SCALEWIDTH(32)))
        cont.setImage(UIImage(named: "Add-friends"), for: .normal)
        cont.layer.cornerRadius = SCALEWIDTH(16)
        cont.addTarget(self, action: #selector(clickAddFriendBtn), for: .touchUpInside)
        return cont
    }()
    
    lazy var followBtn:UIButton = {
        let cont = UIButton(frame: CGRectMake(0, 0, SCALEWIDTH(79), SCALEWIDTH(32)))
        cont.setTitle("关注", for: .normal)
        cont.setTitle("已关注", for: .selected)
        cont.setTitleColor(.white, for: .normal)
        cont.setBackgroundImage(UIImage(named: "Rectangle"), for: .normal)
        cont.titleLabel?.font = .medium(14)
        cont.layer.cornerRadius = SCALEWIDTH(16)
        cont.addTarget(self, action: #selector(clickFollowBtn), for: .touchUpInside)
        return cont
    }()
    
    
    lazy var lineView:UIView = {
        let cont = UIView()
        cont.backgroundColor = .rgba(238, 238, 238, 1)
        return cont
    }()

    lazy var aboutBtn:UIButton = {
        let cont = UIButton(frame: CGRectMake(0, 0, SCALEWIDTH(79), SCALEWIDTH(32)))
        cont.setTitleColor(.rgba(118, 118, 118, 1), for: .normal)
        cont.setTitleColor(.rgba(32, 92, 255, 1), for: .selected)
        cont.titleLabel?.font = .medium(16)
        cont.addTarget(self, action: #selector(changeButton(_:)), for: .touchUpInside)
        return cont
    }()
    
    lazy var aboutMineBtn:UIButton = {
        let cont = UIButton(frame: CGRectMake(0, 0, SCALEWIDTH(79), SCALEWIDTH(32)))
       
        cont.setTitleColor(.rgba(118, 118, 118, 1), for: .normal)
        cont.setTitleColor(.rgba(32, 92, 255, 1), for: .selected)
        cont.titleLabel?.font = .medium(16)
        cont.addTarget(self, action: #selector(changeButton(_:)), for: .touchUpInside)
        return cont
    }()
    
    lazy var aboutLineView:UIView = {
        let cont = UIView()
        cont.backgroundColor = .rgba(32, 92, 255, 1)
        return cont
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
